package com.example.fwcz.config;

import com.example.fwcz.entity.user;
import com.example.fwcz.mapper.UserMapper;
import com.example.fwcz.service.impl.UserServiceImpl;
import com.example.fwcz.config.JwtTokenFilter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;

@Configuration
@EnableWebSecurity // open the function of the WebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    UserServiceImpl userService;
//    @Autowired
//    MyAuthenticationEntryPoint myAuthenticationEntryPoint;

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception{
        auth.userDetailsService(userService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception{
        // --- 关闭 csrf 开启 cors ---
        http.cors().and().csrf().disable()
        // --- 关闭 会话为无状态模式 ---
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
        // --- 未授权时指向该页面时的操作 ---
                .exceptionHandling()
                .authenticationEntryPoint(
                        (request,responce,ex)->{
                            responce.sendError(
                                    HttpServletResponse.SC_UNAUTHORIZED,
                                    ex.getMessage()
                            );
                        }
                )
                .and()
        // --- set permissions on endpoints
                .authorizeRequests()
                //.antMatchers("/login/**").permitAll()
                .antMatchers("/admin/**").hasRole("admin")
                .anyRequest().permitAll()
                .and()
                //.anthenticated() 报错
        // --- add JWT token filter
                .addFilterBefore(
                        // --- 对登录对象进行身份认证
                        new JwtLoginFilter("/login",authenticationManager()),
                        UsernamePasswordAuthenticationFilter.class
                )
                .addFilterBefore(
                        // --- 对登录成功的对象进行鉴权
                        new JwtTokenFilter(),
                        UsernamePasswordAuthenticationFilter.class
                );

    }

}
